/*
NAME: 
DESCRIPTION: 备份原稿层;
PARAMETER:
	[
		{
			name : 'prefix',
			title : '前缀名',
			type : 'LineEdit',
			property : {tool_tip : '前缀名,默认是org-'},
		},
		{
			name:"step",
			title:"step名称",
			type:"LineEdit",
			property:{tool_tip:"step名称,默认orig"}
		},
		{
			name : 'suffix',
			title : '后缀名',
			type : 'LineEdit',
			property : {tool_tip : '后缀名,默认没有'},
		},
		{
			name : 'auto_save',
			title : '自动保存',
			type : 'RadioBox',
			property : {
				item_list:[
					{name:'yes',text:'YES'},
					{name:'no',text:'NO'},
				],
				tool_tip:'是否自动保存料号开关'
			}
		}
	]
	
 VERSION_HISTORY:
	V1.00 2021-04-08 Scott Sun
		1.新版本
		
 HELP:
	  <html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		<p> 备份原稿层 </ p>
		<br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<p> step信息 </p>
		<br>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		<p> 无 </p>
		<br>
	</body></html>	
*/

// 引入模块 包 外部参数
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
var mode = $.ikm ? "topcam" : "aimdfm";
var IKM = $.ikm ? $.ikm : require('topcam.ikm6')($);
var GEN = $.gen;
var Job = $.job || $.job_name;
var JobId = $.job_id;
var db = $.db || IKM.db;
var PAR = {};
if ($.par) {
	PAR = $.par;
} else if ($.hasOwnProperty('script_parameter')) {
	PAR = JSON.parse($.script_parameter);
}
if (mode === "aimdfm") {
	IKM.crud("updateRow", {
		table: "pdm_aimdfm_task",
		data: { current_process_title: $.process_title },
		where: { id: $.task_id }
	});
}
var Status = 'ok';
var resultData = [];
var par = PAR; // 接收参数
var default_par = { // 设置默认参数
	step: "orig",
	auto_save: "No",
	prefix: "org-",
	suffix: "",
	units: "mm"
};
for (var key in default_par) {
	if (!par.hasOwnProperty(key) || par[key] == "") {
		par[key] = default_par[key];
	}
}

// 定义变量
var job = Job.toLowerCase();

try {
	// 常规验证 准备工作
	if (!GEN.isJobExists({ job: job })) {
		throw "料号" + job + "不存在";
	}
	if (!GEN.isJobOpen({ job: job })) {
		GEN.openJob({ job: job });
	}
	if (mode == "aimdfm") {
		if (GEN.checkInout({ job: job, mode: "test" }) != 0) {
			throw "the job check";
		}
		GEN.checkInout({ job: job, mode: "out" });
	}
} catch (error) {
	IKM.msg(error);
	return error;
}